import time
import tkinter as  tk
import os
import watchdog.observers
import watchdog.events
import threading

running=True

class FileModifiedHandler(watchdog.events.FileSystemEventHandler):
    def on_modified(self, event):
        print(f" {event.src_path} has been modified.")
        if not event.is_directory:
            print(f"File {event.src_path} has been modified.")

def monitor_file(file_path):
    event_handler = FileModifiedHandler()
    observer = watchdog.observers.Observer()
    observer.schedule(event_handler, path=os.path.dirname(file_path), recursive=False)
    observer.start()
    
    print(f"Monitoring started for {file_path}...")
    while running:
        time.sleep(1)
    
    observer.stop()
    observer.join()
    print(f"Monitoring stopped for {file_path}...")
    

file_path = './x.csv'  # 替换为你的文件路径

with open('./OperatorLog.csv','rt',encoding='utf8') as f:
    lines = f.readlines()
no = 0

def output():
    global no
    open(file_path,'at',encoding='utf8').write(lines[no])
    no += 1

def stop():
    global running
    running = False
 
 
#写一个只有一个按钮一窗口，
root =tk.Tk()
root.title("模拟写入日志")
# 设置窗口大小
root.geometry('200x100')
root.btn = tk.Button(root,text = 'write', command=output)
root.attributes("-topmost",True)
root.btn.pack()
# root.btnStop = tk.Button(root,text = 'stop', command=stop)
# root.btnStop.pack()

# threadMonitor = threading.Thread( target=monitor_file,args=(file_path,))
# threadMonitor.start()

root.mainloop()

